from django.db import models


class Admin(models.Model):
    """ 管理员 """
    username = models.CharField(verbose_name="用户名", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=64)

    def __str__(self):
        return self.username


class Department(models.Model):
    """ 部门表 """
    # id=models.BigAutoField(verbose_name='ID',primary_key=True)#自增
    title = models.CharField(verbose_name='标题', max_length=32)  # verbose_name='标题', 备注用

    # 面向对象，重写了__str__方法
    # 原来的def __str__(self)是 返回对像的描述信息
    # 定制改为了返回的值为 .title
    def __str__(self):
        return self.title


class UserInfo(models.Model):
    """ 员工表 """
    name = models.CharField(verbose_name="姓名", max_length=16)
    password = models.CharField(verbose_name="密码", max_length=64)
    age = models.IntegerField(verbose_name="年龄")
    account = models.DecimalField(verbose_name="账户余额", max_digits=10, decimal_places=2, default=0)
    # create_time = models.DateTimeField(verbose_name="入职时间")  BateTime带时分秒
    create_time = models.DateField(verbose_name="入职时间")

    # 无约束，导致输入混乱
    # depart_id=models.IntegerField(verbose_name="部门id")

    # 1.有约束
    #   - to 关联表
    #   - to_field 关联列
    # 2.django自动
    #   - 写的depart
    #   - 自动生成数据列 depart_id

    # 3.部门表被删除
    # ### 3.1 级联删除
    # depart=models.ForeignKey(to="Department",to_field="id",on_delete=models.CASCADE)
    # ### 3.2 置空
    #
    # 4.跨表取数据
    # 赋值语句是 depart_id=1
    # obj.depart_id 获取的是数据库存储的那个字段值       ”1“
    # obj.depart.title    获取的是根据id查表(to="Department"中的)获取的depart对象"title"  ”销售部“
    depart = models.ForeignKey(verbose_name="部门", to="Department", to_field="id", null=True, blank=True,
                               on_delete=models.SET_NULL)

    # 在jdango中做约束
    # 赋值语句是 gender=1
    # obj.get_gender_display 获取的是gender_choices的对应对象 ”男“
    # obj.gender             获取的是gender值                ”1“
    gender_choices = (
        (1, "男"),
        (0, "女")
    )
    gender = models.SmallIntegerField(verbose_name="性别", choices=gender_choices)


class PrettyNum(models.Model):
    """ 靓号表 """
    mobile = models.CharField(verbose_name="手机号", max_length=11)
    price = models.DecimalField(verbose_name="价格", max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    level_choices = (
        (1, "钻石号"),
        (2, "白金号"),
        (3, "黄金号"),
        (4, "白银号"),
    )
    level = models.SmallIntegerField(verbose_name="级别", choices=level_choices, default=4)
    status_chioces = (
        (0, "未占用"),
        (1, "已使用"),
    )
    status = models.SmallIntegerField(verbose_name="状态", choices=status_chioces, default=0)


class Task(models.Model):
    """ 任务 """
    level_choices = (
        (1, "紧急"),
        (2, "重要"),
        (3, "临时"),
    )
    level = models.SmallIntegerField(verbose_name="级别", choices=level_choices, default=1)
    title = models.CharField(verbose_name="标题", max_length=64)
    detail = models.TextField(verbose_name="详细信息")
    user = models.ForeignKey(verbose_name="负责人", to='Admin', on_delete=models.CASCADE)


class Order(models.Model):
    """ 订单管理 """
    oid = models.CharField(verbose_name="订单号", max_length=64)
    title = models.CharField(verbose_name="名称", max_length=32)
    price = models.IntegerField(verbose_name="价格", )

    status_choices = (
        (1, "待支付"),
        (2, "已支付"),
    )
    status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=1)

    admin = models.ForeignKey(verbose_name="管理员", to='Admin', on_delete=models.CASCADE)


class Boss(models.Model):
    """ 老板 """
    name = models.CharField(verbose_name="姓名", max_length=32)
    age = models.IntegerField(verbose_name="年龄")
    img = models.CharField(verbose_name="头像", max_length=128)


class City(models.Model):
    """ 城市 """
    name = models.CharField(verbose_name="名称", max_length=32)
    people = models.IntegerField(verbose_name="人口")
    # FileField 本质上也是CharField路径，Django可以自动保存数据，到media的'city/'目录
    img = models.FileField(verbose_name="Logo", max_length=128, upload_to='city/')
